
package com.example.filetransfer.publicsource.fileutils;

import java.io.File;
import java.util.List;

public class DiffFileTask implements Runnable {
    private DirDiffer mDirDiffer;
    private XmlReader mXmlReader;
    private String mTreeFilePath;
    private String mComparePath;
    private List<MyFile> mChangedFile;
    private List<MyFile> mDeletedFile;
    private List<MyFile> mNewFile;
    private DiffEndListener mListener;

    public interface DiffEndListener {
        public void onDiffEnd();
    }

    public List<MyFile> getNewFile() {
        return mNewFile;
    }

    public List<MyFile> getChangedFile() {
        return mChangedFile;
    }

    public List<MyFile> getDeletedFile() {
        return mDeletedFile;
    }

    public DiffFileTask(String comparePath, String treefile, DiffEndListener listener) {
        mDirDiffer = new DirDiffer();
        mXmlReader = new XmlReader();
        mTreeFilePath = treefile;
        mComparePath = comparePath;
        mListener = listener;
    }

    @Override
    public void run() {
        mXmlReader.parserXml(mTreeFilePath);
        Dir treeFileDir = mXmlReader.getRootDir();
        treeFileDir.clearUp();
        Dir comparePathDir = DirUtils.scanDir(null, new File(mComparePath));
        comparePathDir.clearUp();
        mDirDiffer.compareDirFile(comparePathDir, treeFileDir);
        mChangedFile = mDirDiffer.getChangedFiles();
        mDeletedFile = mDirDiffer.getNewFiles();
        mNewFile = mDirDiffer.getDeletedFils();
        if (mListener != null) {
            mListener.onDiffEnd();
        }
    }

    public static void main(String[] args) {
        new DiffFileTask("/new_disk2/sync2", "/new_disk2/sync/.filetree.xml", null).run();
    }
}
